Skip to content

fix: support device allocation for multi-container with init containers#1650

Merged
hami-robot[bot] merged 6 commits intoProject-HAMi:masterfrom
haitwang-cloud:fix/multi-container-init-container-device-allocation
Apr 2, 2026
Merged

fix: support device allocation for multi-container with init containers#1650
hami-robot[bot] merged 6 commits intoProject-HAMi:masterfrom
haitwang-cloud:fix/multi-container-init-container-device-allocation

Conversation

@haitwang-cloud
Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug
What this PR does / why we need it:
This commit fixes the 'device request not found' error that occurs when
allocating devices to multi-container pods that contain init containers.

Problem:

  • When a pod has both init containers and regular containers, the device
    allocation index mapping was incorrect
  • The annotation format uses ';' to separate containers, but the code was
    not properly handling the index offset between init containers and
    regular containers
  • Empty ContainerDevices entries were being skipped, breaking the index
    mapping

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

@hami-robot hami-robot bot added kind/bug Something isn't working dco-signoff: no labels Feb 11, 2026
@hami-robot hami-robot bot added the size/M label Feb 11, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @haitwang-cloud, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves issues related to device allocation in Kubernetes pods that utilize both init containers and regular containers. The changes ensure that device requests are correctly processed and mapped to the appropriate containers by fixing indexing logic and preventing the premature skipping of container device entries. This improves the robustness of device allocation for complex pod configurations.

Highlights

  • Fixes device allocation for multi-container pods with init containers: Addressed the 'device request not found' error that occurred when allocating devices to pods containing both init containers and regular containers.
  • Corrected container index mapping: Implemented logic to properly map device requests to init containers and regular containers, accounting for the index offset between them.
  • Preserved empty ContainerDevices entries: Ensured that empty ContainerDevices entries are no longer skipped during decoding, which was previously breaking the index mapping for multi-container pods.
  • Enhanced pending pod detection: Modified the logic to identify pending pods for device binding, now also considering pods in the 'DeviceBindSuccess' phase, which is crucial for multi-container scenarios where some containers might be allocated while others are still pending.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • pkg/device-plugin/nvidiadevice/nvinternal/plugin/util.go
    • Implemented logic to differentiate between init containers and regular containers when retrieving device requests.
    • Adjusted container indexing to correctly map device requests to the appropriate container type (init or regular).
  • pkg/device/devices.go
    • Modified DecodePodDevices to ensure empty ContainerDevices entries are not skipped, preserving the correct index mapping for multi-container pods.
    • Refactored Resourcereqs to correctly initialize PodDeviceRequests to account for both init and regular containers.
    • Separated the processing of resource requests for init containers and regular containers, applying an offset for regular containers.
  • pkg/util/util.go
    • Modified GetPendingPod to also consider pods in the DeviceBindSuccess phase as 'pending' for device binding, alongside DeviceBindAllocating, to support multi-container scenarios where some containers might be allocated while others are not.
Activity
  • No human activity has occurred on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@haitwang-cloud haitwang-cloud force-pushed the fix/multi-container-init-container-device-allocation branch from ae0f945 to 904f282 Compare February 11, 2026 05:39
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a bug in device allocation for multi-container pods that include init containers. The changes correctly handle container indexing for both init and regular containers, prevent skipping of empty device entries to maintain index integrity, and adjust the logic for identifying pending pods in multi-container scenarios. The fixes appear correct and well-targeted. I've included a couple of suggestions to improve code maintainability and readability.

@haitwang-cloud
Copy link
Copy Markdown
Contributor Author

U can reproduce the bug by the following yaml

          initContainers:
            - name: cuda-sample-vector-add
              image: nvcr.io/nvidia/k8s/cuda-sample:vectoradd-cuda11.7.1-ubuntu20.04
              command:
                - /bin/bash
                - '-c'
                - '--'
              args:
                - /cuda-samples/vectorAdd && exit 0 || exit 1
              resources:
                limits:
                  hami.com/gpu: '1'
              imagePullPolicy: IfNotPresent
          containers:
          - name: cuda-sample-vector-add
            image: nvcr.io/nvidia/k8s/cuda-sample:vectoradd-cuda11.7.1-ubuntu20.04
            command:
              - /bin/bash
              - '-c'
              - '--'
            args:
                - /cuda-samples/vectorAdd && exit 0 || exit 1
            resources:
              limits:
                hami.com/gpu: '1'
            imagePullPolicy: IfNotPresent

Copy link
Copy Markdown
Contributor

@DSFans2014 DSFans2014 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix the failed test

@hami-robot hami-robot bot added size/L and removed size/M labels Feb 24, 2026
@haitwang-cloud
Copy link
Copy Markdown
Contributor Author

@DSFans2014 Done,PTAL again

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 52.32% <100.00%> (+0.42%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...vice-plugin/nvidiadevice/nvinternal/plugin/util.go 26.45% <100.00%> (+5.59%) ⬆️
pkg/device/devices.go 91.27% <100.00%> (+9.28%) ⬆️
pkg/util/util.go 74.82% <100.00%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@haitwang-cloud
Copy link
Copy Markdown
Contributor Author

@Shouren @DSFans2014 @archlitchi PTAL, and let me know if u have any concerns

haitwang-cloud and others added 4 commits March 25, 2026 15:25
…tainers

Signed-off-by: Tim <tim.wang03@sap.com>
Remove trailing whitespace from blank lines in the Resourcereqs
function to maintain consistent code style.

Signed-off-by: Tim <tim.wang03@sap.com>
Update test cases to reflect actual decode behavior where trailing ";"
in annotation format produces an extra empty ContainerDevices element.

- Add explicit 'want' field to separate input args from expected output
- Document the encode/decode roundtrip behavior in test comments
- Fix assertion to compare against expected 'want' values instead of args
- Add missing empty ContainerDevices to Test_DecodePodDevices expected output

Signed-off-by: Tim <tim.wang03@sap.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: Tim <tim.wang03@sap.com>
@haitwang-cloud haitwang-cloud force-pushed the fix/multi-container-init-container-device-allocation branch from 6a6d46b to 5f4d1d1 Compare March 25, 2026 07:26
…t containers

Signed-off-by: Tim <tim.wang03@sap.com>
@hami-robot hami-robot bot added size/XL and removed size/L labels Mar 25, 2026
@haitwang-cloud
Copy link
Copy Markdown
Contributor Author

@Shouren PTAL again, I have address all the UT error.

@Shouren
Copy link
Copy Markdown
Member

Shouren commented Mar 27, 2026

@Shouren PTAL again, I have address all the UT error.

@haitwang-cloud I am attending KubeCon EU this week. I will review it next week.

Copy link
Copy Markdown
Member

@Shouren Shouren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@hami-robot hami-robot bot added the lgtm label Apr 2, 2026
@hami-robot
Copy link
Copy Markdown
Contributor

hami-robot bot commented Apr 2, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: haitwang-cloud, Shouren

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@hami-robot hami-robot bot added the approved label Apr 2, 2026
@hami-robot hami-robot bot merged commit 0658b5c into Project-HAMi:master Apr 2, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants